<!-- src/components/NoticeDetailModal.vue -->
 <!-- //第四步创建公告详情模态框组件 -->
<template>
  <div class="modal-overlay" @click.self="close">
    <div class="modal-content">
      <div class="modal-header">
        <h3>{{ notice.title }}</h3>
        <button class="close-btn" @click="close">×</button>
      </div>

      <div class="notice-detail">
        <div class="notice-meta">
          <span>发布时间: {{ formatDate(notice.createTime) }}</span>
          <span v-if="notice.expireTime">过期时间: {{ formatDate(notice.expireTime) }}</span>
          <span>目标群体: {{ notice.targetGroup ? notice.targetGroup.join(', ') : '全部' }}</span>
        </div>

        <div class="notice-content">
          <p>{{ notice.content }}</p>
        </div>
      </div>

      <div class="modal-actions">
        <button class="btn-close" @click="close">关闭</button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue';
import { noticeApi } from '@/api/notice';

export default {
  name: 'NoticeDetailModal',
  props: {
    noticeId: {
      type: Number,
      required: true
    }
  },
  emits: ['close'],
  setup(props, { emit }) {
    const notice = ref({});
    const loading = ref(false);

    const loadNoticeDetail = async () => {
  loading.value = true;
  try {
    const response = await noticeApi.getNoticeDetail(props.noticeId);
    if (response.data.code === '000000') {
      const notice = response.data.data;
      // 转换 targetGroup
      if (notice.targetGroup) {
        try {
          notice.targetGroup = JSON.parse(notice.targetGroup);
        } catch (e) {
          console.error('解析 targetGroup 失败:', e);
          notice.targetGroup = [];
        }
      } else {
        notice.targetGroup = [];
      }
      notice.value = notice;
    } else {
      console.error('获取公告详情失败:', response.data.msg);
    }
  } catch (error) {
    console.error('加载公告详情失败:', error);
  } finally {
    loading.value = false;
  }
};

    const formatDate = (dateString) => {
      if (!dateString) return '';
      try {
        return new Date(dateString).toLocaleString('zh-CN');
      } catch (error) {
        return dateString;
      }
    };

    const close = () => {
      emit('close');
    };

    onMounted(() => {
      loadNoticeDetail();
    });

    return {
      notice,
      loading,
      formatDate,
      close
    };
  }
};
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 8px;
  width: 700px;
  max-width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #eee;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #999;
}

.close-btn:hover {
  color: #666;
}

.notice-detail {
  padding: 20px;
}

.notice-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 15px;
  margin-bottom: 20px;
  color: #7f8c8d;
  font-size: 14px;
}

.notice-content {
  line-height: 1.8;
  color: #34495e;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  padding: 20px;
  border-top: 1px solid #eee;
}

.btn-close {
  padding: 10px 20px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

@media (max-width: 768px) {
  .modal-content {
    width: 90vw;
  }
  
  .notice-meta {
    flex-direction: column;
    gap: 5px;
  }
}
</style>